home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / DIMISC.C < prev    next >
C/C++ Source or Header  |  1995-09-19  |  6KB  |  175 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    dimisc.c
  5. //   Title:    Data File I/O Library
  6. //   Notice:    John M. Weeder
  7. //   NOTE:      Data File also added to module Z4SDBLD with 1 line of code
  8. //        added to fix a problem with this module.  This is the only
  9. //        module that needed the fix to the library was left with the
  10. //        original code.  The fix does cause a suspisios pointer
  11. //        conversion but that is ok as it is fine.
  12. //                 Copyright (c) 1993. All rights reserved.
  13. //             This module contains proprietary information and should be 
  14. //                treated as confidential.
  15. //
  16. //----------------------------------------------------------------------------
  17. //                           MAINTENANCE HISTORY
  18. //
  19. // $Workfile$
  20. // $Revision$
  21. //   $Author$
  22. //     $Date$
  23. //      $Log$    
  24. //
  25. //----------------------------------------------------------------------------
  26. //                             MODULE NARRATIVE
  27. //
  28. //
  29. //    This module contains support routines for data files.
  30. //
  31. //    The code in this module should be written entirely in C. 
  32. //    Do not use any C++ constructs.
  33. //
  34. //    This module is portable to:
  35. //        DOS 3.X+
  36. //        MS Windows 3.X+
  37. //        OS/2 2.X+
  38. //        OS/2 2.0 PM
  39. //        SCO UNIX.
  40. //
  41. //    The following compilers are supported:
  42. //        MSC 6.0A
  43. //        MSC/C++ 7.0
  44. //        Borland C++ 3.1 for DOS
  45. //        Borland C++ 1.0 for OS/2 2.X
  46. //        SCO UNIX cc
  47. //
  48. //----------------------------------------------------------------------------
  49. #include <di.h>
  50.  
  51.  
  52. //----------------------------------------------------------------------------
  53. //   Description:    Check if a file is a valid data file.
  54. //    Parameters:    pcsz File name
  55. //       Returns:    TRUE if valid data file.
  56. //----------------------------------------------------------------------------
  57. BOOL FN_E DioIsDataFile(PCSZ pcsz, PCSZ pcszAppId)
  58. {
  59.     CHAR szName[MAX_PATH];
  60.     USHORT fs = FL_OPEN|FL_BINARY|FL_READONLY|FL_DENYNONE;
  61.     HF hf;
  62.     FPOS fsize;
  63.     DATAHDR hdr;
  64.  
  65.     //
  66.     //    Create full name of file and verify existence.
  67.     //
  68.     Assert(pcsz && pcsz[0]);
  69.     strcpy(szName, pcsz);
  70.     FnameAppendExt(szName, di.szDataExt, TRUE);
  71.     if (!FnameQualify(szName, di.szDataExt, di.pcszDataPath, FNAME_NO_CREATE))
  72.         return FALSE;
  73.  
  74.     if (!FileOpen(&hf, szName, fs, NULL))
  75.         return FALSE;
  76.     //
  77.     //    Go to great extents to verify that this is an actual
  78.     //    and uncorrupted data file. This is the only place that the
  79.     //    header data is verified. Directory entries are verified as
  80.     //    they are read.
  81.     //
  82.  
  83.     fsize = FileGetSize(hf);
  84.  
  85.     if (fsize < sizeof(DATAHDR)
  86.     || (fsize % sizeof(DATAHDR)) != 0)
  87.         {
  88.         FileClose(hf);
  89.         return FALSE;
  90.         }
  91.     if (!FileRead(hf, (PBYTE)&hdr, sizeof(DATAHDR), 0L))
  92.         {
  93.         FileClose(hf);
  94.         return FALSE;
  95.         }
  96.     FileClose(hf);
  97.     strcpy((PSZ)pcszAppId,"ZIP4");
  98.     if (hdr.lId != DFH_ID
  99.     || hdr.usVersion < DFH_VERION
  100.     || hdr.crc != (ULONG)CrcCalc((PBYTE)&hdr, sizeof(DATAHDR) - sizeof(ULONG)))
  101.         return FALSE;
  102.  
  103.     if (pcszAppId && memcmp(hdr.szAppId, pcszAppId, sizeof(hdr.szAppId)) != 0)
  104.         return FALSE;
  105.  
  106.     return TRUE;
  107. }
  108.  
  109.  
  110. //----------------------------------------------------------------------------
  111. //   Description:    Check if file pointer is at end of file.
  112. //    Parameters:    hlf        Logical file handle
  113. //       Returns:    TRUE if at EOF
  114. //----------------------------------------------------------------------------
  115. BOOL FN_E DioIsEof(HLF hlf)
  116. {
  117.     Assert(hlf >= 0 && hlf < MAX_LOGICAL_FILES);
  118.     Assert(di.logical[hlf].fUsed);
  119.     return (di.logical[hlf].fl & LF_EOF) != 0;
  120. }
  121.  
  122.  
  123. //----------------------------------------------------------------------------
  124. //   Description:    Check if file is in error state.
  125. //    Parameters:    hlf        Logical file handle
  126. //       Returns:    TRUE if file is in an error state
  127. //----------------------------------------------------------------------------
  128. BOOL FN_E DioIsError(HLF hlf)
  129. {
  130.     HPF hpf;
  131.  
  132.     if (hlf < 0 || hlf >= MAX_LOGICAL_FILES || !di.logical[hlf].fUsed)
  133.         return TRUE;
  134.     hpf = di.logical[hlf].hpf;
  135.     return (di.logical[hlf].fl & LF_ERROR) || (di.physical[hpf].fl & PF_ERROR);
  136. }
  137.  
  138.  
  139. //----------------------------------------------------------------------------
  140. //   Description:    Check if file is a valid logical file.
  141. //    Parameters:    pcsz        Logical file name.
  142. //                                    This name may contain an explicit phyiscal file
  143. //                                    name by placing it prior to the logical name
  144. //                                    and separating it using the tilde character:
  145. //                                        "physical~logical"
  146. //                        usType     File type to open.
  147. //       Returns:    TRUE if logical file name is valid
  148. //----------------------------------------------------------------------------
  149. BOOL FN_E DioIsLogicalFile(PCSZ pcsz, USHORT usType)
  150. {
  151.     HPF hpf;
  152.     SIZET cDir;
  153.     DATADIR dir;
  154.                                                     // Find logical file
  155.     return DioFindLogical(pcsz, usType, &hpf, &cDir, &dir);
  156. }
  157.  
  158.  
  159. //----------------------------------------------------------------------------
  160. //   Description:    Check if file pointer is valid
  161. //    Parameters:    hlf        Logical file handle
  162. //       Returns:    TRUE if at valid
  163. //----------------------------------------------------------------------------
  164. BOOL FN_E DioIsValid(HLF hlf)
  165. {
  166.     return hlf >= 0
  167.         && hlf < MAX_LOGICAL_FILES
  168.         && di.logical[hlf].fUsed
  169.         && (di.logical[hlf].fl & LF_ERROR) == 0
  170.         && (di.physical[di.logical[hlf].hpf].fl & PF_ERROR) == 0;
  171. }
  172. //----------------------------------------------------------------------------
  173. //------------------------------- End of File --------------------------------
  174. //----------------------------------------------------------------------------
  175.